package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: 赵勇镔
 * @Time: 2021/5/17 21:25
 * @Description:
 */

@RestController
@RequestMapping("/api/item")
public class ItemApiController {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;


    @ApiOperation("数据汇总 页面所需要的4部分数据")
    @GetMapping("/getItem/{skuId}")
    public Map<String,Object> getItem(@PathVariable Long skuId){
        Map<String,Object> result = new HashMap<>();


        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //通过商品id获取库存信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);//因为三个都要用它的返回值 所有不能直接串联 而是获取返回对象 给三个同时使用


        CompletableFuture<Void> voidCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {//thenAcceptAsync取一个新线程 不延用上一个
            //获取123级分类
            BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);


        CompletableFuture<Void> voidCompletableFuture1 = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {//thenAcceptAsync取一个新线程 不延用上一个
            //销售属性值及选中红边框
            List<SpuSaleAttr> spuSaleAttrValueChecked = productFeignClient.
                    getSpuSaleAttrValueChecked(skuInfo.getId(), skuInfo.getSpuId());
            result.put("spuSaleAttrList", spuSaleAttrValueChecked);
        }, threadPoolExecutor);


        CompletableFuture<Void> valuesSkuJson1 = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {//thenAcceptAsync取一个新线程 不延用上一个
            //销售属性值组合对应库存id
            Map valuesSkuJson = productFeignClient.getValuesSkuJson(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(valuesSkuJson));//把map转化为Json格式
        }, threadPoolExecutor);


        //并行线程
        CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.runAsync(() -> {
            //单独价格
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", JSONObject.toJSONString(price));
        }, threadPoolExecutor);

        //并行线程 增加热度分
        CompletableFuture.runAsync(() -> {
            //增加热度分
            listFeignClient.hotScore(skuId);
        }, threadPoolExecutor);



        //主线程要等所有线程结束再返回
        CompletableFuture.allOf(voidCompletableFuture, voidCompletableFuture1,
                valuesSkuJson1,voidCompletableFuture2).join();

        return  result;
    }


}
